package com.example.wechattakeaway.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.service.UserService;
import com.example.wechattakeaway.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class PhoneCode {

    private static String code;

    @Autowired
    UserService userService;

    private static UserService userService1;
    @PostConstruct
    public void init2(){
        userService1 = this.userService;
    }

    /**
     * 阿里云短信服务配置
     *
     * @return
     */
    public static ResultVO getPhonemsg(String userPhone) {

        /*
         * 进行关系校验
         */
        System.out.println(userPhone);
        if (userPhone == null || userPhone.equals("")) {
            System.out.println("手机号为空");
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        //获取验证码
        code = vcode();
        RedisProvider redisProvider = new RedisProvider();
        Jedis jedis = redisProvider.getJedis();

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FndQcNn52csbi1zJNHg", "LNivAZlac05NmLpVj1x1y8N4fCYfn1");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userPhone);
        request.putQueryParameter("SignName", "Lockmemory");
        request.putQueryParameter("TemplateCode", "SMS_186965614");
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        request.putQueryParameter("OutId", code);
        try {
            System.out.println("333333333333333333");
            CommonResponse response = client.getCommonResponse(request);
            jedis.setex(userPhone + "Code", 300, code);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        } catch (ClientException e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    /**
     * 生成6位随机数验证码
     *
     * @return
     */
    public static String vcode() {
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 9);
        }
        return vcode;
    }


    /*
    验证
     */
    public static ResultVO CheckCode(String userPhone, String usercode) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tJdiKcPmUmfav9YLXNh", "Xj2ewuslEoDkLiVbJa5BfI8vVFB7Nr");
        IAcsClient client = new DefaultAcsClient(profile);

        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyyMMdd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
//        System.out.println("现在时间：" + sdf.format(date));  输出已经格式化的现在时间（24小时制）
        String NowTime = sdf.format(date);

        RedisProvider redisProvider = new RedisProvider();
        Jedis jedis = redisProvider.getJedis();
        if (jedis.exists(userPhone + "Code") == null) {
            return ResultVOUtil.error(ResultEnum.MESSAGE_NOTSEND);
        }

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("QuerySendDetails");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumber", userPhone);
        request.putQueryParameter("SendDate", NowTime);
        request.putQueryParameter("PageSize", "1");
        request.putQueryParameter("CurrentPage", "1");
        try {
            CommonResponse response = client.getCommonResponse(request);

            System.out.println(response.getData());
//            response.getData().contains("\"OutId\":"+)
        } catch (ServerException e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        } catch (ClientException e) {
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        if (!jedis.get(userPhone + "Code").equals(usercode)) {
            System.out.println(jedis.get(userPhone + "Code"));
            System.out.println(usercode);
            jedis.set(usercode + "Code", "nil");
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }
}
